//
//  MarketTypeCollectionViewController.swift
//  Nahuo
//
//  Created by zang qilong on 2017/5/20.
//  Copyright © 2017年 zang qilong. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class MarketTypeCollectionViewController: UICollectionViewController {
    
    var typeProductDict = [String: [Product]]()
    var currentType = "2"
    var fetchProductRequest:FetchMarketProducts = FetchMarketProducts(type: "2", page: 0)

    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView?.register(UINib.init(nibName: ProductTypeCollectionViewCell.reuseID(), bundle: nil), forCellWithReuseIdentifier: ProductTypeCollectionViewCell.reuseID())
        self.collectionView?.backgroundColor = UIColor.tableBGColor()
        self.collectionView?.contentInset = UIEdgeInsetsMake(10, 10, 0, 10)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // public Method 
    
    func fetchProcut(type:String, isRefresh:Bool) {
        currentType = type
        fetchProductRequest.type = type
        fetchProductRequest.startFetchProducts {
            [unowned self]void in
            self.typeProductDict[type] = self.fetchProductRequest.productArray
            self.collectionView!.reloadData()
        }
    }


    // MARK: UICollectionViewDataSource

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        
        return 1
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if let productArray = typeProductDict[currentType] {
            return productArray.count
        }
        return 0
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ProductTypeCollectionViewCell.reuseID(), for: indexPath) as! ProductTypeCollectionViewCell
    
        if let productArray = typeProductDict[currentType] {
            cell.setupCellWithProductModel(model: productArray[indexPath.row])
        }

        return cell
    }

    // MARK: UICollectionViewDelegate

}

private let kNamelabelHeight:CGFloat = 30
private let kImageViewNameLabelSpacing:CGFloat = 15
private let kfavouriteButtonHeight:CGFloat = 19
private let kNameLabelButtonSpacing:CGFloat = 15
private let kbuttonBottomSpacing:CGFloat = 10

extension MarketTypeCollectionViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = UIScreen.main.bounds.size.width
        let cellWidth = (width - 30)/2
        let cellHeight = cellWidth + kImageViewNameLabelSpacing + kNamelabelHeight + kNameLabelButtonSpacing + kfavouriteButtonHeight + kbuttonBottomSpacing
        return CGSize(width: cellWidth, height: cellHeight)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10;
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 10;
    }
    
}
